package ejecutable;

import java.awt.Color;
import java.awt.EventQueue;
import sensores.*;

import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTabbedPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.ImageIcon;

public class MJX {

	private  JFrame frmSistemaDeControl;
	private JTextField lactometroTextField;
	private JTextField RedTextField;
	private JTextField GreenTextField;
	private JTextField BlueTextField;
	private JTextField potenciometroTextField;
	
	private final Lactometro lactometro;
	private final Potenciometro potenciometro;
	private final SensorCMOS sensorCMOS;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MJX window = new MJX();
					window.frmSistemaDeControl.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public MJX() {
		this.lactometro = new Lactometro();
		this.potenciometro = new Potenciometro();
		this.sensorCMOS = new SensorCMOS();
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmSistemaDeControl = new JFrame();
		frmSistemaDeControl.setTitle("Sistema de Control de Alimentación Bovina");
		frmSistemaDeControl.setBounds(100, 100, 800, 600);
		frmSistemaDeControl.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmSistemaDeControl.getContentPane().setLayout(null);
		
		JLabel tituloLbl = new JLabel("Sistema de Control de Alimentación Bovina");
		tituloLbl.setIcon(null);
		tituloLbl.setFont(new Font("Dialog", Font.PLAIN, 34));
		tituloLbl.setBounds(12, 27, 752, 89);
		frmSistemaDeControl.getContentPane().add(tituloLbl);
		
		JTabbedPane panelPrincipal = new JTabbedPane(JTabbedPane.TOP);
		panelPrincipal.setBounds(304, 128, 482, 434);
		frmSistemaDeControl.getContentPane().add(panelPrincipal);
		
		JPanel sensoresTab = new JPanel();
		panelPrincipal.addTab("Sensores", null, sensoresTab, null);
		sensoresTab.setLayout(null);
		
		JLabel lactometroLbl = new JLabel("Lactómetro");
		lactometroLbl.setBounds(24, 55, 97, 15);
		sensoresTab.add(lactometroLbl);
		
		lactometroTextField = new JTextField();
		lactometroTextField.setEditable(false);
		lactometroTextField.setBounds(146, 51, 97, 19);
		sensoresTab.add(lactometroTextField);
		lactometroTextField.setColumns(10);
		
		JLabel unidadLactometroLbl = new JLabel("g/L");
		unidadLactometroLbl.setBounds(252, 53, 34, 15);
		sensoresTab.add(unidadLactometroLbl);
		
		JLabel lblSensorCmos = new JLabel("Sensor CMOS");
		lblSensorCmos.setBounds(24, 97, 97, 15);
		sensoresTab.add(lblSensorCmos);
		
		RedTextField = new JTextField();
		RedTextField.setEditable(false);
		RedTextField.setColumns(10);
		RedTextField.setBounds(167, 95, 48, 19);
		sensoresTab.add(RedTextField);
		
		JLabel lblR = new JLabel("R");
		lblR.setBounds(146, 97, 15, 15);
		sensoresTab.add(lblR);
		
		GreenTextField = new JTextField();
		GreenTextField.setEditable(false);
		GreenTextField.setColumns(10);
		GreenTextField.setBounds(248, 97, 48, 19);
		sensoresTab.add(GreenTextField);
		
		JLabel lblG = new JLabel("G");
		lblG.setBounds(227, 99, 15, 15);
		sensoresTab.add(lblG);
		
		BlueTextField = new JTextField();
		BlueTextField.setEditable(false);
		BlueTextField.setColumns(10);
		BlueTextField.setBounds(330, 97, 48, 19);
		sensoresTab.add(BlueTextField);
		
		JLabel lblB = new JLabel("B");
		lblB.setBounds(309, 99, 15, 15);
		sensoresTab.add(lblB);
		
		JLabel potenciometroLbl = new JLabel("pH-metro");
		potenciometroLbl.setBounds(24, 143, 97, 15);
		sensoresTab.add(potenciometroLbl);
		
		potenciometroTextField = new JTextField();
		potenciometroTextField.setEditable(false);
		potenciometroTextField.setColumns(10);
		potenciometroTextField.setBounds(146, 141, 97, 19);
		sensoresTab.add(potenciometroTextField);
		
		JButton btnSensar = new JButton("Sensar");
		btnSensar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				lactometroTextField.setText(Integer.toString(lactometro.getValorSensado()));
				potenciometroTextField.setText(Double.toString(potenciometro.getValorSensado()));
				Color colorSensado = sensorCMOS.getValorSensado();
				RedTextField.setText(Integer.toString(colorSensado.getRed()));
				GreenTextField.setText(Integer.toString(colorSensado.getGreen()));
				BlueTextField.setText(Integer.toString(colorSensado.getBlue()));

			}
		});
		btnSensar.setBounds(348, 177, 117, 25);
		sensoresTab.add(btnSensar);
		
		JPanel diagnosticoTab = new JPanel();
		panelPrincipal.addTab("Diagnóstico", null, diagnosticoTab, null);
		
		JPanel plandealimentacionTab = new JPanel();
		panelPrincipal.addTab("Plan de Alimentación", null, plandealimentacionTab, null);
		
		JPanel estadisticasTab = new JPanel();
		panelPrincipal.addTab("Estadísticas", null, estadisticasTab, null);
		
		JLabel versionLbl = new JLabel("MJX version 0.0000001");
		versionLbl.setBounds(12, 12, 170, 15);
		frmSistemaDeControl.getContentPane().add(versionLbl);
		
		JTabbedPane vacaPanel = new JTabbedPane(JTabbedPane.TOP);
		vacaPanel.setBounds(12, 128, 280, 277);
		frmSistemaDeControl.getContentPane().add(vacaPanel);
		
		JPanel panel = new JPanel();
		vacaPanel.addTab("Vaca", null, panel, null);
		
		JLabel label = new JLabel("");
		label.setBounds(39, 417, 223, 152);
		frmSistemaDeControl.getContentPane().add(label);
		label.setIcon(new ImageIcon(MJX.class.getResource("/resources/vaca.png")));
	}
}
